//首页
(function(){
	if (location.pathname.indexOf('/delivery') >= 0) {
		//获取所有社区
		var projects = $("#hiddenP").data('projects');
		var projList = [];
		if (projects) {
			for(var i = 0; i < projects.length; i++){
				var obj = {};
				obj['name'] = projects[i].cityName + '-' + projects[i].name;
				obj['content'] = '/delivery/' + projects[i].id + '/' + projects[i].name;
				projList.push(obj);
			}
		}
		// 设置菜单
	    if (window.ClientBridge && ClientBridge.setMenus) {
	      ClientBridge.setMenus(projList);
	    }

	    swig.setFilter('deliveryStatusFilter', function (input) {
		  if (input == '0') {
		  	return '待处理';
		  }else if (input == '1') {
		  	return '已接单';
		  }else if (input == '2') {
		  	return '配送中'
		  }else if (input == '3') {
		  	return '已完成待评价'
		  }else if (input == '4') {
		  	return '已完成已评价'
		  }else if (input == '5') {//用户
		  	return '已取消未归还'
		  }else if (input == '6') {
		  	return '已取消已归还'
		  }else if (input == '7') {
		  	return '超时取消'
		  }else if (input == '8') {//管家
		  	return '已取消未归还';
		  }
		  else{
		  	return ''
		  }
		});

		//配送时间段
		swig.setFilter('postPeriodFilter', function (input) {
		  if (input == '0') {
		    return '上午';
		  }else if (input == '1') {
		    return '下午';
		  }else if (input == "2") {
		  	return "晚上";
		  } else{
		    return ''
		  }
		});

	      // 未抢单翻页
		loadMoreFactory({
		    size: 20,
		    url: '/delivery/ajax/unsign',
		    panel: '#unsign',
		    template: '#unsignTempl',
		    before: function () {
		      return $('#unsign').is('.active');
		    }
		});

		  // 已抢单翻页
		loadMoreFactory({
		    size: 20,
		    url: '/delivery/ajax/signed',
		    panel: '#signed',
		    template: '#signedTempl',
		    before: function () {
		      return $('#signed').is('.active');
		    }
		});

		  // 已取消翻页
		loadMoreFactory({
		    size: 20,
		    url: '/delivery/ajax/signcancel',
		    panel: '#signcancel',
		    template: '#signcancelTempl',
		    before: function () {
		      return $('#signcancel').is('.active');
		    }
		});


		//带抢单
		$('.dely-unsign').click(function(){
			location.href = '/delivery/?status=' + '0';
		})

		//已抢单
		$('.dely-signed').click(function(){
			location.href = '/delivery/?status=' + '1';
		})

		//取消
		$('.dely-signcancel').click(function(){
			location.href = '/delivery/?status=' + '2';
		})

		// $('.bar-header >h1').click(function(){
		// 	alert(111)
		// })

	    //抢单
	    $("#unsign").on('click','.grabexpress',function(){
	    	var self = this;
	    	$(self).addClass('disabled');
	    	var curId = $(this).data('grabid');
	    	var projectId = $(this).data('projectid');
	    	// var pathname = location.origin + '/delivery/' + 'success/' + curId;
	    	
	    	$.post('/delivery/ajax/grab',{
	    		id:curId,
	    		// url:pathname,
	    		projectId:projectId
	    	}).then(function(res){
	    		if (res.data.entity.code == 0) {
	    			Modal.alert({
	    				message:"抢单成功",
	    				callback:function(){
	    					location.reload();
	    				}
	    			});
	    		}else{
	    			Modal.alert(res.data.entity.message);
	    			$(self).removeClass('disabled');
	    		}
	    	})
	    })

	    //取消配送
	    $("#signed").on('click','.cancelHK',function(){
	    	var self = this;
	    	var curId = $(this).data('curid');
	    	Modal.confirm({
	    		message:'取消“已接单”状态的配送单，将同时取消您本日继续抢单的资格，确认要取消吗？',
	    		callback:function(){
	    			$(self).addClass('disabled');
	    			$.post('/delivery/ajax/cancelHK',{
			    		id:curId
			    	}).then(function(res){
			    		if (res.code == 200) {
			    			Modal.alert({
			    				message:"取消配送成功",
			    				callback:function(){
			    					location.reload();
			    				}
			    			});
			    		}else{
			    			Modal.alert(res.data.message);
			    			$(self).removeClass('disabled');
			    		}
			    	})
	    		}
	    	})
	    	
	    })

	    //扫码领取
	    $("#signed").on('click','.scanner-get',function(){
	    	var code = $(this).attr('data-code');
	    	localStorage.setItem('delivery_code', code); 
	    	ClientBridge.scanner(false);
	    	$(document).triggerHandler('event:scanner', function(data){});
	    })

	    //扫码签收
	    $('#signed').on('click','.scaner-qianshou',function(){
	    	var curId = $(this).data('curid');
	    	location.href = "/delivery/code/" + curId;
	    })

	    //扫码归还
	    $("#signcancel").on('click','.scaner-back',function(){
	    	var code = $(this).attr('data-code');
	    	localStorage.setItem('delivery_code', code); 
	    	ClientBridge.scanner(false);
	    	$(document).triggerHandler('event:scanner', function(data){
		        // console.log(data);
	        });
	    })
	}
})();

//配送单详情
(function(){
	if (location.pathname.indexOf('/delivery/expressd') >= 0) {
		var curId = $(this).data('curid');

		 //抢单
	    $(".grabexpress").on('click',function(){
	    	var self = this;
	    	$(self).addClass('disabled');
	    	var curId = $(this).data('grabid');
	    	var projectId = $(this).data('projectid');
	    	// var pathname = location.origin + '/delivery/' + 'success/' + curId;
	    	
	    	$.post('/delivery/ajax/grab',{
	    		id:curId,
	    		// url:pathname,
	    		projectId:projectId
	    	}).then(function(res){
	    		if (res.data.entity.code == 0) {
	    			Modal.alert({
	    				message:"抢单成功",
	    				callback:function(){
	    					location.href = '/delivery/?status=1';
	    				}
	    			});
	    		}else{
	    			Modal.alert(res.data.entity.message);
	    			$(self).removeClass('disabled');
	    		}
	    	})
	    })


		//取消配送
		$(".cancelHK").click(function(){
			var self = this;
	    	Modal.confirm({
	    		message:'取消“已接单”状态的配送单，将同时取消您本日继续抢单的资格，确认要取消吗？',
	    		callback:function(){
	    			$(self).addClass('disabled');
	    			$.post('/delivery/ajax/cancelHK',{
			    		id: curId
			    	}).then(function(res){
			    		if (res.code == 200) {
			    			Modal.alert({
			    				message:"取消配送成功",
			    				callback:function(){
			    					location.reload();
			    				}
			    			});
			    		}else{
			    			Modal.alert(res.data.message);
			    			$(self).removeClass('disabled');
			    		}
			    	})
	    		}
	    	})
		})

		//扫码领取
	    $(".scanner-get").click(function(){
	    	var code = $(this).attr('data-code');
	    	localStorage.setItem('delivery_code', code); 
	    	ClientBridge.scanner(false);
	    	$(document).triggerHandler('event:scanner', function(data){});
	    })

	    //扫码签收
	    $('.scaner-qianshou').click(function(){
	    	var curId = $(this).data('curid');
	    	location.href = "/delivery/code/" + curId;
	    })

	    //扫码归还
	    $(".scaner-back").click(function(){
	    	var code = $(this).attr('data-code');
	    	localStorage.setItem('delivery_code', code); 
	    	ClientBridge.scanner(false);
	    	$(document).triggerHandler('event:scanner', function(data){
		        // console.log(data);
	        });
	    })

	    //取消配送
	    $('.cancel-dispatch').click(function(){
	    	var self = this;
	    	var curId = $(this).data('curid');
	    	Modal.confirm({
	    		message:'取消“已接单”状态的配送单，将同时取消您本日继续抢单的资格，确认要取消吗？',
	    		callback:function(){
	    			$(self).addClass('disabled');
	    			$.post('/delivery/ajax/cancelHK',{
			    		id:curId
			    	}).then(function(res){
			    		if (res.code == 200) {
			    			Modal.alert({
			    				message:"取消配送成功",
			    				callback:function(){
			    					location.reload();
			    				}
			    			});
			    		}else{
			    			Modal.alert(res.data.message);
			    			$(self).removeClass('disabled');
			    		}
			    	})
	    		}
	    	})
	    })
	}
})()

